/* 
 * File:   TLC549X.c
 * Author: Twistx77
 *
 * Created on 29 de noviembre de 2012, 16:09
 */

#include "TLC49X.h"
uint8_t firstCycleFlag = 1;


//Output is inverted thats why is initilialized to 4095
uint16_t gsValues[16] = {
    4095,4095, 4095, 4095,
    4095, 4095, 4095, 4095,
    4095, 4095, 4095, 4095,
    4095, 4095, 4095, 4095
};


uint8_t dcValues [16] = {
    63, 63, 63, 63,
    63, 63, 63, 63,
    63, 63, 63, 63,
    63, 63, 63, 63

};


//Initializes outputs for correct operation of the TLC594X device

void TLC594X_Init(void) {

    //GSCLK = 0;
    SCLK = 0;
    XLAT = 0;
    BLANK = 1;
    MODE = 0;
    XHALF = 1;

}


// Sets value of Gray Scale

void TLC594X_SetGrayScaleValue(uint8_t channel, uint16_t value) {

    gsValues[16-channel] = value;

}

 void TLC594X_LoadGrayScaleValues(void) {
        uint8_t i,x;
        uint16_t valueBuffer;
        MODE = 0;
        BLANK = 1;
        BLANK = 0;

        for (i = 0; i < NUMBER_OF_CHANNELS; i++) {
            valueBuffer = gsValues[i];
            for (x = 0; x < PWM_RESOLUTION; x++) {
                if(valueBuffer & 2048)
                    SIN = 1;
                else
                    SIN = 0;
                valueBuffer <<= 1;
                SCLK = 1;
                SCLK = 0;
                
            }
        }
        //BLANK = 1;

        XLAT = 1;       
        XLAT = 0;
        SCLK = 1;
        SCLK = 0;

        T4CONbits.TMR4ON = 1;
    }
// Refreshes the outputs with the values assigend to gsValues array
/*
void TLC594X_LoadGrayScaleValues(void) {
    uint8_t i, x;
    uint16_t valueBuffer, GSCLK_Counter, Data_Counter;
    GSCLK_Counter = 0;
    Data_Counter = 0;
    MODE = 0;
    T4CONbits.TMR4ON = 0;



    BLANK = 0;

    for (;;) {
        if (GSCLK_Counter > 4095) {
            BLANK = 1;
            XLAT = 1;
            XLAT = 0;
            if (firstCycleFlag){
                SCLK = 1;
                 SCLK = 0;
                firstCycleFlag = 0;
            }
            break;
        } else {
            if (!(Data_Counter >  192 - 1)) {
                if (gsData[Data_Counter])
                    SIN = 1;
                else
                    SIN = 0;
                SCLK = 1;
                SCLK = 0;
                Data_Counter++;
            }
            GSCLK =1;
            GSCLK = 0;
            GSCLK_Counter++;

}

      T4CONbits.TMR4ON = 1;




    }
}
    *//*
    void TLC594X_LoadGrayScaleValues(void) {
        uint8_t i,x;
        uint16_t valueBuffer;
        MODE = 0;

        for (i = 0; i < NUMBER_OF_CHANNELS; i++) {
            valueBuffer = gsValues[i];
            for (x = 0; x < PWM_RESOLUTION; x++) {
                if(valueBuffer & 1)
                SIN = 1;
                else
                    SIN = 0;
                valueBuffer >>= 1;
                SCLK = 1;
                SCLK = 0;
            }
        }
        SCLK = 1;
                SCLK = 0;
                SCLK = 1;
                SCLK = 0;
        XLAT = 1;
        XLAT = 0;
    }

     */
    // Refreshes the outputs with the values assigend to dcValues

    void TLC594X_LoadDotCorrectionValues(void) {
        uint8_t i, x;
        uint16_t valueBuffer;
        MODE = 0;

        for (i = 0; i < NUMBER_OF_CHANNELS; i++) {
            valueBuffer = gsValues[i];
            for (x = 0; x < PWM_RESOLUTION; x++) {
                SIN = valueBuffer & 1;
                valueBuffer >>= 1;
                SCLK = 1;
                SCLK = 0;
            }

        }
        XLAT = 1;
        XLAT = 0;
    }
